<?php

class AuthController extends Yaf_Controller_Abstract {
	
	public function init() {
        $this->db = Singleton::getDb();
	}

	public function uptokenAction(){
        success(['token'=>'211']);exit();
    }
    
	public function loginAction(){
		$post = $this->getRequest()->getPost();
		if(!isset($post['username']) || !$post['username']){
			error('用户名不能为空');
		}
		if(!isset($post['password']) || !$post['password']){
			error('密码不能为空');
		}
		$user = $this->db->get('admin_users','*',['username'=>$post['username']]);
		$password = $post['password'];
		if(!$user || $user['password'] != $password){
			error('用户名或密码错误');
		}
		$token = $user['id'].'|'.md5(time().uniqid().'wxlion');
		$ip = $this->getRequest()->getServer('REMOTE_ADDR');
		$info = $this->getRequest()->getServer('HTTP_USER_AGENT');
		$info = substr($info, 0,100);
		$this->db->insert('tokens',['token'=>$token,'user_id'=>$user['id'],'ip'=>$ip,'info'=>$info,'created_at'=>date('Y-m-d H:i:s')]);
		success($token);
	}
}
